package com.scalar.chiptrack.operations.dataentry.lotcorrection;

import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.ArrayList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.struts.util.LabelValueBean;

/**
 * User: Ganesh
 * Date: Dec 20, 2006
 * Time: 4:41:16 PM
 */
public class LotCorrectionHelper  implements Constants
{
    private LotCorrectionHelper()
    {
    }
    public static LotCorrectionHelper getInstance()
    {
        return new LotCorrectionHelper();
    }

    public ArrayList getProductList( String processType ) throws ScalarException
    {
        ArrayList partNumberList;
        PartNumber partNumber;
        ArrayList partNumbersLableValueBeans = new ArrayList();
        if( processType == null || "0".equalsIgnoreCase ( processType ) )
        {
            partNumbersLableValueBeans = ProductDao.loadAllBaseProducts( null );
        }
        else if ( "Bump Store".equalsIgnoreCase( processType ))
        {
            partNumberList = ProductDao.getPartNumberBySpecificProcess ( "Wafer Store" );
            partNumbersLableValueBeans = new ArrayList();
            int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
            for ( int i = 0; i < partNumberListSize; i++ )
            {
                partNumber = ( PartNumber )partNumberList.get(i);
                if( "Wafer Store".equalsIgnoreCase( partNumber.getProcessTypeName() ) && "Bump Store".equalsIgnoreCase( partNumber.getProcessType() ) )
                {
                    partNumbersLableValueBeans.add ( new LabelValueBean( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
                }
            }
        }
        else if ( "Back Grind".equalsIgnoreCase( processType ))
        {
            partNumberList = ProductDao.getPartNumberBySpecificProcess( "Assembly" );
            partNumbersLableValueBeans = new ArrayList();
            int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
            for ( int i = 0; i < partNumberListSize; i++ )
            {
                partNumber = ( PartNumber ) partNumberList.get(i);

                if( "Assembly".equalsIgnoreCase ( partNumber.getProcessTypeName() ) && "Back Grind".equalsIgnoreCase( partNumber.getProcessType() ) )
                {
                    partNumbersLableValueBeans.add ( new LabelValueBean( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
                }
            }
        }
        else
        {
            partNumberList = ProductDao.getPartNumber ( processType );
            int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
            for ( int i = 0; i < partNumberListSize; i++ )
            {
                partNumber = ( PartNumber ) partNumberList.get ( i );
                partNumbersLableValueBeans.add ( new LabelValueBean ( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
            }
        }
        return partNumbersLableValueBeans;
    }

    public ArrayList getLocationList( String processStepId ) throws ScalarException
    {
        VendorLocation location ;
        ArrayList locationList;
        ArrayList lotNewLocationsLableValueBeans= new ArrayList();
        locationList = DataEntryDAO.getSelectedLocations( Integer.parseInt( processStepId ) );

        for ( int i = 0; i < locationList.size(); i++ )
        {
            location = (VendorLocation) locationList.get(i);
            lotNewLocationsLableValueBeans.add(new LabelValueBean(location.getLocationName(), location.getLocationID()));
        }
       return lotNewLocationsLableValueBeans;
    }


    public ArrayList getNewLocationList( String processStepId, String locationId ) throws ScalarException
   	{
        VendorLocation location ;
        ArrayList locationList;
        ArrayList lotNewLocationsLableValueBeans= new ArrayList();

        locationList = DataEntryDAO.getSelectedLocations( Integer.parseInt( processStepId ) );

        int locationListSize = locationList != null ? locationList.size() : 0;
        for( int i = 0; i < locationListSize; i++ )
        {
            location = ( VendorLocation ) locationList.get(i);
            if(!(locationId.equalsIgnoreCase(location.getLocationID()) ) )
            {
                lotNewLocationsLableValueBeans.add(new LabelValueBean(location.getLocationName(), location.getLocationID()));
            }
        }
        return lotNewLocationsLableValueBeans;
    }

    public  ArrayList getLotNumbersList( String processStepId, String locationId ) throws ScalarException
    {
        Lot lot;
        ArrayList lotList;
        ArrayList lotCorrectionLotNumberLabelValueBeans   = new ArrayList();
        lotList = DataEntryDAO.getAllLotNumbersByPSTypeName( processStepId, locationId );
        int lotListSize = lotList != null ? lotList.size() : 0;
        for( int i = 0; i < lotListSize; i++ )
        {
            lot = (Lot) lotList.get(i);
            lotCorrectionLotNumberLabelValueBeans.add(new LabelValueBean( lot.getLotNumber(), lot.getLotId()));
        }
        return lotCorrectionLotNumberLabelValueBeans;
    }

     public  ArrayList getLotCategoryType()
    {
        ArrayList lotCategoryList = new ArrayList();
        lotCategoryList.add( new LabelValueBean( "PROD", "PROD" ) );
        lotCategoryList.add( new LabelValueBean( "ENGG", "ENGG" ) );

        return lotCategoryList;
    }

    public ArrayList getProcessList()
    {
        ArrayList processList = new ArrayList();
        processList.add( new LabelValueBean( "FAB", "Fab" ) );
        processList.add( new LabelValueBean( "FAB OPTION", "Fab Option" ) );
        processList.add( new LabelValueBean( "WAFER STORE", "Wafer Bank" ) );
        processList.add( new LabelValueBean( "DIE BANK", "Die Bank" ) );
        processList.add( new LabelValueBean( "ASSEMBLY", "Assembly" ) );
        processList.add( new LabelValueBean( "FPO", "FPO" ) );
        processList.add( new LabelValueBean( "FT", "FT" ) );
        processList.add( new LabelValueBean( "CLASS STORE", "Tape" ) );
        processList.add( new LabelValueBean( "FINISHED GOODS", "FG" ) );

        return processList;
    }
     public static ArrayList  getNewProductList ( DBTransaction dbTransaction, int productType, String processType, boolean withOutObsoleteProducts, boolean returnWithLabelValueBean, String defaultProcessStepId, int parentProductId ) throws ScalarException
    {
        Connection conn             = null;
        ResultSet rs                = null;
        PreparedStatement pstmt     = null;
        ArrayList partNumberList    = null;
        PartNumber partNumber       = null;
        String partNo               = null;
        String processStepId        = null;

        try
        {
            //System.out.println("    productType = " + productType + "    processType = " + processType + "    withOutObsoleteProducts = " + withOutObsoleteProducts + "    returnWithLabelValueBean = " + returnWithLabelValueBean + "    defaultProcessStepId = " + defaultProcessStepId );

            partNumberList = new ArrayList();
            if ( dbTransaction == null )
            {
                conn = DataSource.getConnection();
            }
            else
            {
                conn = dbTransaction.getConnection();
            }

            pstmt = conn.prepareStatement( "{CALL SP_LOAD_ALL_PART_NUMBERS_BY_PROCESS( ?, ?, ?, ? ) }" );

            pstmt.setString ( 1, processType);                                  // HERE 0 MEANS ALL PROCESS PRODUCTS OTHERWISE FETCH ONLY PARTICULAR PROCESS PRODUCTS
            pstmt.setInt    ( 2, productType);                                  // HERE 0 = ALL( DIE+BOARD+KIT ), 1 = ONLY DIE , 2 = ONLY BOM
            pstmt.setInt    ( 3, ( withOutObsoleteProducts ? 0 : 1 ) );         // HERE 0 NOT OBSOLETE PART NO'S ONLY OTHERWISE LOAD ALL TYPES OF PART NO'S
            pstmt.setInt    ( 4, parentProductId );                             // HERE 0 MEANS ALL BASE PRODUCTS OTHERWISE FETCH ONLY GIVEN BASE PRODUCT PART NUMBERS

            rs = pstmt.executeQuery();

            while ( rs.next() )
            {
                partNo          = rs.getString( "PART_NUMBER" );
                processStepId   =  rs.getString( "PROCESSSTEP_ID" );
                if( !defaultProcessStepId.trim().equalsIgnoreCase(processStepId.trim()))
                {
                    partNumberList.add( new LabelValueBean(partNo, processStepId) );
                }
            }
        }
        catch ( SQLException sql)
        {
            sql.printStackTrace();
            throw new ScalarException( "Could not get Part Numbers by type", sql );
        }
        finally
        {
            if ( rs != null )
            {
                try { rs.close(); } catch (SQLException igonore) { }
            }

            if ( pstmt != null )
            {
                try { pstmt.close(); } catch (SQLException igonore) { }
            }

            if ( conn != null )
            {
                try { conn.close(); } catch (SQLException igonore) { }
            }
        }
        return partNumberList;
        /*   ArrayList partNumberList;
        PartNumber partNumber;
        ArrayList newPartNumbersLableValueBeans = new ArrayList();
        if ( "Bump Store".equalsIgnoreCase( processType ))
        {
            partNumberList = ProductDao.getPartNumberBySpecificProcess( "Wafer Store" );
            newPartNumbersLableValueBeans = new ArrayList();
            int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
            for( int i = 0; i < partNumberListSize; i++ )
            {
                partNumber = ( PartNumber )partNumberList.get(i);
                if( "Wafer Store".equalsIgnoreCase( partNumber.getProcessTypeName() ) && "Bump Store".equalsIgnoreCase( partNumber.getProcessType() ) && (!(processStepId.equalsIgnoreCase(partNumber.getProcessStepId()))))
                {
                    newPartNumbersLableValueBeans.add ( new LabelValueBean( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
                }
            }
        }
        else if ( "Back Grind".equalsIgnoreCase( processType ))
        {
            partNumberList = ProductDao.getPartNumberBySpecificProcess( "Assembly" );
            newPartNumbersLableValueBeans = new ArrayList();
            int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
            for( int i = 0; i < partNumberListSize; i++ )
            {
                partNumber = ( PartNumber ) partNumberList.get(i);
                if( "Assembly".equalsIgnoreCase ( partNumber.getProcessTypeName() ) && "Back Grind".equalsIgnoreCase( partNumber.getProcessType() ) && (!(processStepId.equalsIgnoreCase(partNumber.getProcessStepId()))) )
                {
                    newPartNumbersLableValueBeans.add ( new LabelValueBean( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
                }
            }
        }
        else
        {
            if ( processType != null && !"0".equals(processType) )
            {
                partNumberList = ProductDao.getPartNumber ( processType );
                int partNumberListSize = partNumberList != null ? partNumberList.size() : 0;
                for ( int i = 0; i < partNumberListSize; i++ )
                {
                    partNumber = ( PartNumber ) partNumberList.get ( i );
                    if(!(processStepId.equalsIgnoreCase(partNumber.getProcessStepId())))
                    {
                        newPartNumbersLableValueBeans.add ( new LabelValueBean ( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
                    }
                }
            }
        }
        return newPartNumbersLableValueBeans;       */
    }
}

